package com.yanqu.road.server.manger.maidian.mammon;

import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserMallBuyCount;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.UserTaskData;
import com.yanqu.road.logic.bussiness.player.UserBagBussiness;
import com.yanqu.road.logic.bussiness.player.UserMallBuyBussiness;
import com.yanqu.road.logic.bussiness.player.UserTaskBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.mall.MallModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.TaskMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MammomLog {

    private static String GOD = "god";
    private static String GOD_ACHIEVE = "god_achieve";
    private static String GOD_STORE = "god_store";
    private static String GOD_NUM = "god_num";
    private static String GOD_TYPE = "god_type";
    private static String GOD_TYPENUM = "god_typenum";

    private static String ITEM_ID = "item_id";
    private static String ISGET = "isget";


    public static void trackSnapshot(GamePlayer player){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;


            DefaultEventTraceAction action = new DefaultEventTraceAction(GOD, player);

            //成就
            Map<Integer, Integer> typeValueMap = new HashMap<>();
            List<Map<String, Object>> conditionGroupList = new ArrayList<>();
            if(player != null){
                for (Map.Entry<Integer, BaseTaskData> taskDataEntry : player.getModule(TaskModule.class).getBaseTaskDataMap().entrySet()) {
                    BaseTaskData baseTaskData = taskDataEntry.getValue();
                    if (baseTaskData.getUserTaskData() == null) {
                        continue;
                    }
                    BaseCondition baseCondition = baseTaskData.getBaseCondition();
                    if(baseCondition == null){
                        //DataAnalyticsMgr.getLogger().error(" player {}, taskType {} ", player.getUserId(), baseTaskData.getUserTaskData() != null ? baseTaskData.getUserTaskData().getTaskType() : "");
                        continue;
                    }
                    TaskConditionInfo taskConditionInfo = baseCondition.getTaskConditionInfo();
                    if(taskConditionInfo == null){
                        continue;
                    }
                    if(taskConditionInfo.getType() == eGamePlayerEventType.MammonDecorationCount.getValue()){
                        int type = taskConditionInfo.getParamList().get(1).intValue();
                        int num = baseTaskData.getUserTaskData().getValue().intValue();
                        int old = typeValueMap.getOrDefault(type, 0);
                        if(num > old){
                            typeValueMap.put(type, num);
                        }
                    }
                }
            }else {
                Map<Integer, UserTaskData> userTaskDataMap = UserTaskBussiness.getUserTaskDataMap(player.getUserId());
                for (Map.Entry<Integer, UserTaskData> dataEntry : userTaskDataMap.entrySet()) {
                    UserTaskData userTaskData = dataEntry.getValue();
                    TaskConditionInfo conditionInfo = TaskMgr.getTaskConditionById(userTaskData.getTaskId());
                    if(conditionInfo == null || conditionInfo.getType() != eGamePlayerEventType.MammonDecorationCount.getValue()){
                        continue;
                    }
                    int type = conditionInfo.getParamList().get(1).intValue();
                    int num = userTaskData.getValue().intValue();
                    int old = typeValueMap.getOrDefault(type, 0);
                    if(num > old){
                        typeValueMap.put(type, num);
                    }
                }
            }
            for (Map.Entry<Integer, Integer> entry : typeValueMap.entrySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put(GOD_TYPE, entry.getKey());
                map.put(GOD_TYPENUM, entry.getValue());
                conditionGroupList.add(map);
            }

            action.build(GOD_ACHIEVE, conditionGroupList);

            List<Map<String, Object>> buyGroupList = new ArrayList<>();
            //商店
            List<MallInfo> mallInfoList = MallMgr.getMallInfoListByShopType(eShopType.MammonMall.getValue());
            if(player != null){
                MallModule mallModule = player.getModule(MallModule.class);
                for (MallInfo mallInfo : mallInfoList) {
                    int count = mallModule.getMallBuyCount(mallInfo.getMallId());
                    if(count <= 0){
                        continue;
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put(ITEM_ID, mallInfo.getMallId());
                    map.put(ISGET, true);
                    buyGroupList.add(map);
                }
            }else {
                Map<Integer, UserMallBuyCount> mallBuyCountMap = UserMallBuyBussiness.getUserMallBuyCountMap(player.getUserId());
                for (MallInfo mallInfo : mallInfoList) {
                    UserMallBuyCount mallBuyCount = mallBuyCountMap.get(mallInfo.getMallId());
                    if(mallBuyCount == null){
                        continue;
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put(ITEM_ID, mallInfo.getMallId());
                    map.put(ISGET, true);
                    buyGroupList.add(map);
                }
            }
            action.build(GOD_STORE, buyGroupList);

            //财神币
            int itemId = 101036;
            long itemNum = 0;
            if(player != null){
                UserBagItem bagItem = player.getModule(BagModule.class).getUserBagItem(itemId);
                if(bagItem != null){
                    itemNum = bagItem.getNum();
                }
            }else {
                Map<Integer, UserBagItem> map = UserBagBussiness.getUserBagItemMap(player.getUserId());
                if(map.containsKey(itemId)){
                    itemNum = map.get(itemId).getNum();
                }
            }
            action.build(GOD_NUM, itemNum);
            action.trace();

        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }

}
